package com.ltmall.api.parse;

import com.ltmall.api.bean.ActivityPrice;
import com.ltmall.api.comm.Path;
import com.ltmall.api.comm.PathUtil;
import com.ltmall.api.entity.JsonGoodsList;
import com.ltmall.api.entity.JsonOtherGoods;
import com.ltmall.api.manager.GoodsManager;

import java.util.ArrayList;
import java.util.List;

public class GoodsParse {

    //商品列表的解析
    public static List<JsonGoodsList> goodsListParse(List<?> list, int type, GoodsManager goodsManager) {
        List<JsonGoodsList> goodsList = new ArrayList<JsonGoodsList>();
        for (int i = 0; i < list.size(); i++) {
            String goodsFormat;
            Object[] obj = (Object[]) list.get(i);
            JsonGoodsList jGoods = new JsonGoodsList();

            jGoods.setGoodsID(Integer.parseInt(obj[0].toString()));
//			jGoods.setGoodsName((obj[1] == null) ? null : obj[1].toString());
            jGoods.setGoodsPrice(Float.parseFloat(obj[1].toString()));
            jGoods.setGoodsPic(getOneGoodsPicture(obj[2].toString()));
            jGoods.setStoreNumber(Integer.parseInt(obj[3].toString()));
            jGoods.setSellNumber(Integer.parseInt(obj[4].toString()));
            jGoods.setShopID(Integer.parseInt(obj[5].toString()));
            jGoods.setShopName((obj[6] == null) ? null : obj[6].toString());
            jGoods.setGoodsBFPrice(Float.parseFloat(obj[7].toString()));
            jGoods.setGoodsTitle((obj[8] == null) ? null : obj[8].toString());
            jGoods.setGoodsLabel((obj[9] == null) ? null : obj[9].toString());

            goodsFormat = (obj[10] == null) ? null : obj[10].toString();

            //是否是时令果蔬
            if (type == 3) {
                jGoods.setPercent((obj[11] == null) ? 0 : Float.parseFloat(obj[11].toString()));
                jGoods.setStartTime((obj[12] == null) ? null : obj[12].toString());
                jGoods.setEndTime((obj[13] == null) ? null : obj[13].toString());
                jGoods.setDeliveryTime((obj[14] == null) ? null : obj[14].toString());
                jGoods.setActivityPicture(getOneGoodsPicture(obj[15].toString()));

                jGoods.setRemindNumber(goodsManager.remindNumber(Integer.parseInt(obj[0].toString())));
            }

            ActivityPrice ap = goodsManager.getActivityPrice(Integer.parseInt(obj[0].toString()), goodsFormat);
            if (ap != null) {
                jGoods.setGoodsPrice(ap.getStartPrice());//活动价格
                jGoods.setGoodsBFPrice(ap.getEndPrice());//还原的价格
            }

            goodsList.add(jGoods);
        }
        return goodsList;
    }

    //解析带有省市县信息的商品列表
    public static List<JsonGoodsList> goodsListParse(List<?> list, int type, GoodsManager goodsManager, int isNeedAddr) {
        List<JsonGoodsList> goodsList = new ArrayList<JsonGoodsList>();
        for (int i = 0; i < list.size(); i++) {
            String goodsFormat;
            Object[] obj = (Object[]) list.get(i);
            JsonGoodsList jGoods = new JsonGoodsList();

            jGoods.setGoodsID(Integer.parseInt(obj[0].toString()));
//			jGoods.setGoodsName((obj[1] == null) ? null : obj[1].toString());
            jGoods.setGoodsPrice(Float.parseFloat(obj[1].toString()));
            jGoods.setGoodsPic(getOneGoodsPicture(obj[2].toString()));
            jGoods.setStoreNumber(Integer.parseInt(obj[3].toString()));
            jGoods.setSellNumber(Integer.parseInt(obj[4].toString()));
            jGoods.setShopID(Integer.parseInt(obj[5].toString()));
            jGoods.setShopName((obj[6] == null) ? null : obj[6].toString());
            jGoods.setGoodsBFPrice(Float.parseFloat(obj[7].toString()));
            jGoods.setGoodsTitle((obj[8] == null) ? null : obj[8].toString());
            jGoods.setGoodsLabel((obj[9] == null) ? null : obj[9].toString());

            goodsFormat = (obj[10] == null) ? null : obj[10].toString();

            jGoods.setProvince((obj[11] == null) ? null : obj[11].toString());
            jGoods.setCity((obj[12] == null ? null : obj[12].toString()));
            jGoods.setCountry((obj[13] == null ? null : obj[13].toString()));

            //是否是时令果蔬
            if (type == 3) {
                jGoods.setPercent((obj[11] == null) ? 0 : Float.parseFloat(obj[11].toString()));
                jGoods.setStartTime((obj[12] == null) ? null : obj[12].toString());
                jGoods.setEndTime((obj[13] == null) ? null : obj[13].toString());
                jGoods.setDeliveryTime((obj[14] == null) ? null : obj[14].toString());
                jGoods.setActivityPicture(getOneGoodsPicture(obj[15].toString()));

                jGoods.setProvince((obj[16] == null) ? null : obj[16].toString());
                jGoods.setCity((obj[17] == null ? null : obj[17].toString()));
                jGoods.setCountry((obj[18] == null ? null : obj[18].toString()));


                jGoods.setRemindNumber(goodsManager.remindNumber(Integer.parseInt(obj[0].toString())));
            }

            ActivityPrice ap = goodsManager.getActivityPrice(Integer.parseInt(obj[0].toString()), goodsFormat);
            if (ap != null) {
                jGoods.setGoodsPrice(ap.getStartPrice());//活动价格
                jGoods.setGoodsBFPrice(ap.getEndPrice());//还原的价格
            }

            goodsList.add(jGoods);
        }
        return goodsList;
    }

    //其他商品
    public static List<JsonOtherGoods> otherGoodsListParse(List<?> list) {
        List<JsonOtherGoods> goodsList = new ArrayList<JsonOtherGoods>();
        for (int i = 0; i < list.size(); i++) {
            Object[] obj = (Object[]) list.get(i);
            JsonOtherGoods jGoods = new JsonOtherGoods();

            jGoods.setGoodsID(Integer.parseInt(obj[0].toString()));
            jGoods.setGoodsPicture(getOneGoodsPicture((obj[1] == null) ? null : obj[1].toString()));
            jGoods.setGoodsPrice(Float.parseFloat(obj[2].toString()));
            jGoods.setGoodsTitle((obj[3] == null) ? null : obj[3].toString());

            goodsList.add(jGoods);
        }
        return goodsList;
    }


    //返回商品所有的图片的路径
    public static String getAllGoodsPicture(String picPaths) {
        return PathUtil.getPicPaths(picPaths, Path.GOODS);
    }

    //返回第一张商品图片的
    public static String getOneGoodsPicture(String picPaths) {
        return PathUtil.getPicPath(picPaths, Path.GOODS);
    }

}
